<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Validate;

class Index extends Common
{
    public function index()
    {
    	//banner轮播模块
    	$banner_list = Db::name('article')->where(['is_banner' => 1])->order('sort asc')->select();
    	$this->assign('banner_list', $banner_list);

    	//政策法规模块
	    $zc_type_list = Db::name('type')->field('id,name')->where(['pid' => 3])->order('sort asc')->select();
	    if($zc_type_list){
	        foreach($zc_type_list as &$v){
				$list = Db::name('article')->field('id,title')->where(['two_type_id' => $v['id']])->order('sort asc')->limit(18)->select();
				$v['list'] = splitArray($list, ceil(count($list) / 2));
//				$v['list'] = $list;
	        }
	    }
	    $this->assign('zc_type_list', $zc_type_list ?? []);

	    //最新资讯模块
	    $zxzx_article_list = Db::name('article')->where(['one_type_id' => 44])->order('sort asc')->limit(16)->select();
	    $this->assign('zxzx_article_list', $zxzx_article_list ? splitArray($zxzx_article_list, ceil(count($zxzx_article_list) / 2)) : []);

	    //常见问题解答模块
	    $cjwt_article_list = Db::name('article')->where(['one_type_id' => 47])->order('sort asc')->limit(6)->select();
		$this->assign('cjwt_artile_list', $cjwt_article_list);

		//服务大厅模块
	    $fwdt_article_list = Db::name('article')->where(['one_type_id' => 49])->order('sort asc')->limit(16)->select();
	    $this->assign('fwdt_article_list', $fwdt_article_list ? splitArray($fwdt_article_list, ceil(count($fwdt_article_list) / 2)) : []);

	    //通知公告公示模块
	    $gggs_article_list = Db::name('article')->where(['one_type_id' => 50])->order('sort asc')->select();
	    $this->assign('gggs_article_list', $gggs_article_list);

	    //业务工作模块
	    $business_id = Db::name('type')->where(['is_business' => 1])->column('id');
		$yw_type_list = Db::name('type')->field('id,name')->where(['pid' => $business_id[0]])->order('sort asc')->select();
	    $this->assign('yw_type_list', $yw_type_list);

	    //政府规章模块
	    $zzgz_article_list = Db::name('article')->where(['one_type_id' => 52])->order('sort asc')->limit(7)->select();
	    $this->assign('zzgz_article_list', $zzgz_article_list);

	    //就业失业模块
	    $jysy_article_list = Db::name('article')->where(['one_type_id' => 53])->order('sort asc')->limit(6)->select();
	    $this->assign('jysy_article_list', $jysy_article_list);

		//资讯热点
	    $zxrd_article_list = Db::name('article')->where(['one_type_id' => 54])->order('sort asc')->limit(6)->select();
	    $this->assign('zxrd_article_list', $zxrd_article_list);

	    //表格下载
	    $bgxz_article_list = Db::name('article')->where(['one_type_id' => 55])->order('sort asc')->limit(6)->select();
	    $this->assign('bgxz_article_list', $bgxz_article_list);

    	return $this->fetch();
    }

    public function news()
    {
    	$categoryId = Request()->param('categoryId');
    	$name = Request()->param('search_name');
	    $page = Request()->param('page') ?? 1;
    	$pagesize = 15;

    	$type_info = Db::name('type')->where(['id' => $categoryId])->find();
    	$this->assign('type_info', $type_info);

	    $parent_type_info = Db::name('type')->where(['pid' => $categoryId])->find();
	    $this->assign('parent_type_info', $parent_type_info);

	    //业务工作模块
	    $business_id = Db::name('type')->where(['is_business' => 1])->column('id');
	    $yw_type_list = Db::name('type')->field('id,name')->where(['pid' => $business_id[0]])->order('sort asc')->select();
	    $this->assign('yw_type_list', $yw_type_list);

	    $where =  ' 1 = 1 ';
	    if($name){
		    $where .= ' and (title like "%'. $name .'%")';
	    }else{
		    if($categoryId) $where .= ' and (one_type_id = '. $categoryId .' or two_type_id = '. $categoryId .')';
	    }

	    //文章列表
    	$list = Db::name('article')->where($where)->order('sort asc')->page($page, $pagesize)->select();
		$this->assign('list', $list);

		//总条数
	    $all_count = Db::name('article')->where($where)->order('sort asc')->count();
		//总页数
	    $all_page = ceil($all_count / $pagesize);
		$this->assign('all_page', $all_page > 0 ? $all_page : 1);

    	return $this->fetch();
    }

    /**
     * 文章详情
     * */
    public function article_detail()
    {
	    $id = Request()->param('id');

	    $info = Db::name('article')->where(['id' => $id])->find();
	    $this->assign('info', $info);

	    return $this->fetch();
    }

    /**
     * 表格下载详情
     * */
    public function file_detail()
    {
    	$id = Request()->param('id');

    	$info = Db::name('article')->where(['id' => $id])->find();
    	$this->assign('info', $info);

    	return $this->fetch();
    }

	/**
	 * 职称查询表单
	 * */
	public function jobForm()
	{
		if(Request()->isPost()){
			$input = Request()->param();

			$name = $input['name'];
			$no = $input['no'];
			
			if(empty($name)) apiReturn(2, '请填写姓名');
			if(empty($no)) apiReturn(2, '请填写证书编号');
			
			$count = Db::name('job')->where(['name' => $name, 'no' => $no])->count();
			if($count){
				apiReturn(1, '成功');
			}else{
				apiReturn(2, '未找到相关的信息，请重新输入');
			}
		}else{
			//业务工作模块
			$business_id = Db::name('type')->where(['is_business' => 1])->column('id');
			$yw_type_list = Db::name('type')->field('id,name')->where(['pid' => $business_id[0]])->order('sort asc')->select();
			$this->assign('yw_type_list', $yw_type_list);

			return $this->fetch();
		}
	}

    /**
     * 职称详情
     * */
	public function jobDetail()
	{
		$input = Request()->param();

		$name = $input['name'];
		$no = $input['no'];

		//业务工作模块
		$business_id = Db::name('type')->where(['is_business' => 1])->column('id');
		$yw_type_list = Db::name('type')->field('id,name')->where(['pid' => $business_id[0]])->order('sort asc')->select();
		$this->assign('yw_type_list', $yw_type_list);

		$info = Db::name('job')->where(['name' => $name, 'no' => $no])->find();
		$info['ratify_date'] = time_format(strtotime($info['ratify_date']), 'Y年m月d日');
		$this->assign('info', $info);

		return $this->fetch();
	}

	/**
	 * 职称资格表单
	 * */
	public function senForm()
	{
		if(Request()->isPost()){
			$input = Request()->param();

			$name = $input['name'];
			$no = $input['no'];

			if(empty($name)) apiReturn(2, '请填写姓名');
			if(empty($no)) apiReturn(2, '请填写证书编号');

			$count = Db::name('job')->where(['name' => $name, 'no' => $no])->count();
			if($count){
				apiReturn(1, '成功');
			}else{
				apiReturn(2, '未找到相关的信息，请重新输入');
			}
		}else{
			//业务工作模块
			$business_id = Db::name('type')->where(['is_business' => 1])->column('id');
			$yw_type_list = Db::name('type')->field('id,name')->where(['pid' => $business_id[0]])->order('sort asc')->select();
			$this->assign('yw_type_list', $yw_type_list);

			return $this->fetch();
		}
	}

	/**
	 * 职称资格详情
	 * */
	public function senDetail()
	{
		$input = Request()->param();

		$name = $input['name'];
		$no = $input['no'];

		//业务工作模块
		$business_id = Db::name('type')->where(['is_business' => 1])->column('id');
		$yw_type_list = Db::name('type')->field('id,name')->where(['pid' => $business_id[0]])->order('sort asc')->select();
		$this->assign('yw_type_list', $yw_type_list);

		$info = Db::name('job')->where(['name' => $name, 'no' => $no])->find();
		$info['ratify_date'] = time_format(strtotime($info['ratify_date']), 'Y年m月d日');
		$this->assign('info', $info);

		return $this->fetch();
	}

	/**
	 * 手机端职称查询结果
	 * */
	public function mobileJobDetail()
	{
		$input = Request()->param();

		$id = $input['id'];

		$info = Db::name('job')->where(['id' => $id])->find();
		$info['ratify_date'] = time_format(strtotime($info['ratify_date']), 'Y年m月d日');
		$this->assign('info', $info);

		return $this->fetch();
	}
}
